unit HTTPRequesterUnit;

interface

uses IdHTTP, IdException, System.Classes, System.SysUtils, WARCommons;

{ *
  HTTP Requester is called by WebRequester to Query Armory
  WebRequester call QueryArmory and get back a TQueryArmoryResult


  * }

Type
  THTTPRequester = class(TObject)
  private
    FIdHTTPClient: TIdHTTP;
    FSS: TStringStream;
    FURL: String;
    FLastModified: TDateTime;
    FResponseCode: Integer;
    FErrorMessage: String;
    FResponseText: TStringList;
  public
    constructor Create(Owner: TComponent; AURL: String = ''); // overload;
    destructor Destroy();
    property URL: String read FURL write FURL;
    property LastModified: TDateTime read FLastModified write FLastModified;
    property ResponseCode: Integer read FResponseCode;
    property ErrorMessage: String read FErrorMessage;
    property ResponseText: TStringList read FResponseText;
    procedure GetURL();

  end;

implementation

constructor THTTPRequester.Create(Owner: TComponent; AURL: String = '');
begin

  FIdHTTPClient := TIdHTTP.Create();
  FURL := AURL;
  FSS := TStringStream.Create;
  FResponseText := TStringList.Create;
end;

destructor THTTPRequester.Destroy;
begin
    FIdHTTPClient.Free;
    FSS.Free;
    FResponseText.Free;
    inherited Destroy;
end;

procedure THTTPRequester.GetURL;
begin
  if FURL = '' then
  begin
    FResponseCode := 2;
    FResponseText.Text := '';
    FErrorMessage := 'URL not specified';
    exit;
  end;

  try

    FIdHTTPClient.get(FURL, FSS);
  except
    on E: EIdHTTPProtocolException do
    begin
      FErrorMessage := E.Message;
      FResponseText.Text := FErrorMessage;
      FResponseCode := 3;
    end;
    on E: EIdException do
    begin
      logMessage(E.Message);
      FErrorMessage := E.Message;
      FResponseCode := 3;
    end;
  end;
  if FSS.size > 0 then
  begin
    FSS.Seek(0, soFromBeginning);
    FResponseText.LoadFromStream(FSS, TEncoding.UTF8);
  end;
  FResponseCode := FIdHTTPClient.ResponseCode;
end;

end.
